<?php


namespace sinri\wenli;


use Exception;

class DutyForSBVFromCCRB extends PageReader
{

    /**
     * @var array
     */
    private $context;
    /**
     * @var array
     */
    private $bookMapping;

    public function version()
    {
        return "sbv";
    }

    public function duty()
    {
        $this->fetchBookMapping();
        $this->context = $this->loadContextPage();

        foreach ($this->bookMapping as $bookIndex => $bookMeta) {
            echo "Book Mapping {$bookIndex}th Item: " . json_encode($bookMeta) . PHP_EOL;
//            $chapterCountForBook = $this->bookChapterMapping($bookIndex);
            $chapterCountForBook = $bookMeta['chapter_count'];
            $this->dutyForBookByEachChapter($bookMeta['book_canon_id'], $bookMeta['book_name'], $chapterCountForBook);
        }
    }

    protected function loadContextPage()
    {
        // https://www.ccreadbible.org/Chinese%20Bible/znsigao/1_Samuel_bible_Ch_17_.html
        // https://www.ccreadbible.org/Chinese%20Bible/znsigao/John_bible_Ch_10_.html

        //$context_html=file_get_contents('https://www.ccreadbible.org/Chinese%20Bible/znsigao');
        //file_put_contents(__DIR__.'/../../../html/sbv_ccrb/context.html',$context_html);

//        $context_html=file_get_contents(__DIR__.'/../../../html/sbv_ccrb/context.html');
//        $context=[];
//        // <a data-val="0c2f8f383d3549abc4abd31d846a72cb" href="https://www.ccreadbible.org/Chinese%20Bible/znsigao/Deuteronomy_bible_index.html" data-linktype="internal">申命纪/Deuteronomy</a>
        /*        if(preg_match_all('/<a data-val="[A-Za-z0-9]+" href="(https:\/\/www\.ccreadbible\.org\/Chinese%20Bible\/znsigao\/[A-Za-z0-9_-]+_bible_index.html)" data-linktype="internal" *(title=".+")?>(.+)\/(.+)<\/a>/',$context_html,$matches)){*/
////            print_r($matches);
//
//            for ($i=0;$i<=72;$i++){
//                $context[]=[
//                    'url'=>$matches[1][$i],
//                    'book_cn'=>(($i==52||$i==53) ? '玛':'').$matches[3][$i],
//                    'book_en'=>$matches[4][$i],
//                ];
//
//                $book_detail_page=file_get_contents($matches[1][$i]);
//                $chapter_count=0;
//                if(preg_match_all('/<td><a [^>]+>\d+<\/a><\/td>/',$book_detail_page,$x)){
////                    print_r($x);
//                    $chapter_count=count($x[0]);
//                }
//
//                for($j=0;$j<count($this->bookMapping);$j++){
//                    if($this->bookMapping[$j]['book_name']==(($i==52||$i==53) ? '玛':'').$matches[3][$i]){
//                        $this->bookMapping[$j]['book_url']=$matches[1][$i];
//                        $this->bookMapping[$j]['chapter_count']=$chapter_count;
//                        break;
//                    }
//                }
//            }
//            print_r($this->bookMapping);
//            file_put_contents(__DIR__.'/../../../html/sbv_ccrb/context.json',json_encode($this->bookMapping));
//        }

        $this->bookMapping = json_decode(file_get_contents(__DIR__ . '/../../../html/sbv_ccrb/context.json'), true);
    }

    public function fetchBookMapping()
    {
        $bookNameList = [
            '1' => '创世纪', // ［创］
            '2' => '出谷纪', // ［出］
            '3' => '肋未纪', // ［肋］
            '4' => '户籍纪', // ［户］
            '5' => '申命纪', // ［申］
            '6' => '若苏厄书', // ［苏］
            '7' => '民长纪', // ［民］
            '8' => '卢德传', // ［卢］
            '9' => '撒慕尔纪(上)', // ［撒上］
            '10' => '撒慕尔纪(下)', // ［撒下］
            '11' => '列王纪(上)', // ［列上］
            '12' => '列王纪(下)', // ［列下］
            '13' => '编年纪(上)', // ［编上］
            '14' => '编年纪(下)', // ［编下］
            '15' => '厄斯德拉(上)', // ［厄上］
            '16' => '厄斯德拉(下)', // ［厄下］
            'a' => '多俾亚传', // ［多］
            'b' => '友弟德传', // ［友］
            '17' => '艾斯德尔传', // ［艾］
            'c' => '玛加伯(上)', // ［加上］
            'd' => '玛加伯(下)', // ［加下］
            '18' => '约伯传', // ［约］
            '19' => '圣咏集', // ［咏］
            '20' => '箴言', // ［箴］
            '21' => '训道篇', // ［训］
            '22' => '雅歌', // ［歌］
            'e' => '智慧篇', // ［智］
            'f' => '德训篇', // ［德］
            '23' => '依撒依亚', // ［依］
            '24' => '耶肋米亚', // ［耶］
            '25' => '哀歌', // ［哀］
            'g' => '巴路克', //［巴］　
            '26' => '厄则克耳', // ［则］
            '27' => '达尼尔', // ［达］
            '28' => '欧瑟亚', // ［欧］
            '29' => '岳厄尔', // ［岳］
            '30' => '亚毛斯', // ［亚］
            '31' => '亚北底亚', // ［北］
            '32' => '约纳', // ［纳］
            '33' => '米该亚', // ［米］
            '34' => '纳鸿', // ［鸿］
            '35' => '哈巴谷', // ［哈］
            '36' => '索福尼亚', // ［索］
            '37' => '哈盖', // ［盖］
            '38' => '匝加利亚', // ［匝］
            '39' => '马拉基亚', // ［拉］
//
            '40' => '玛窦福音', // ［玛］
            '41' => '玛尔谷福音', // ［谷］
            '42' => '路加福音', // ［路］
            '43' => '若望福音', // ［若］
            '44' => '宗徒大事录', // ［宗］
            '45' => '罗马书', // ［罗］
            '46' => '格林多前书', // ［格前］
            '47' => '格林多后书', // ［格后］
            '48' => '迦拉达书', // ［迦］
            '49' => '厄弗所书', // ［弗］
            '50' => '斐理伯书', // ［斐］
            '51' => '哥罗森书', // ［哥］
            '52' => '得撒洛尼前书', // ［得前］
            '53' => '得撒洛尼后书', // ［得后］
            '54' => '弟茂德前书', // ［弟前］
            '55' => '弟茂德后书', // ［弟后］
            '56' => '弟铎书', // ［铎］
            '57' => '费肋孟书', // ［费］
            '58' => '希伯来书', // ［希］
            '59' => '雅各伯书', // ［雅］
            '60' => '伯多禄前书', // ［伯前］
            '61' => '伯多禄后书', // ［伯后］
            '62' => '若望一书', // ［若一］
            '63' => '若望二书', // ［若二］
            '64' => '若望三书', // ［若三］
            '65' => '犹达书', // ［犹］
            '66' => '若望默示录', // ［默］
        ];

        $this->bookMapping = [];
        $i = 0;
        foreach ($bookNameList as $bookCanonId => $bookName) {
            $this->bookMapping[] = [
                'book_id' => $i + 1,
                'book_canon_id' => $bookCanonId,
                'book_name' => $bookName,
            ];
            $i++;
        }

    }

    protected function getBookURL($book_id, $book_name = null, $chapter_id = null)
    {
        $book_index = -1;
        for ($i = 0; $i < count($this->bookMapping); $i++) {
            if ($this->bookMapping[$i]['book_canon_id'] == $book_id) {
                $book_index = $i;
                break;
            }
        }
        if ($book_index < 0) throw new Exception(__METHOD__ . '@' . __LINE__);

        $book_url = $this->bookMapping[$book_index]['book_url'];
        $chapter_url = preg_replace('/_index\.html$/', '_Ch_' . $chapter_id . '_.html', $book_url);
        return $chapter_url;
    }

    protected function html2txt($html, $chapter = null)
    {
        $str = '';
        if (preg_match_all('/<sup>(\d+)<\/sup>([^<]+)<\/td>/', $html, $matches)) {
            for ($i = 0; $i < count($matches[0]); $i++) {
                $str .= $chapter . ':' . $matches[1][$i] . ' ' . $matches[2][$i] . PHP_EOL;
            }
        }
        return $str;
    }
}